package utility;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Parser {

	static InputStream is=null;
	static JSONObject jo=null;
	static String str="";
	
	public Parser(){}
	
	public JSONObject getjson(String ur){
		try{
			DefaultHttpClient dh=new DefaultHttpClient();
			HttpUriRequest hp=new HttpGet(ur);
		//	hp.setEntity(new UrlEncodedFormEntity(pr));
			HttpResponse hr=dh.execute(hp);
			HttpEntity he=hr.getEntity();
			is=he.getContent();
		}catch(UnsupportedEncodingException ex){
			ex.printStackTrace();
		}catch(ClientProtocolException e){
			e.printStackTrace();
		}catch(IOException cl){
			cl.printStackTrace();
		}
		
		try{
			BufferedReader br=new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
			StringBuilder sb=new StringBuilder();
			String line=null;
			while((line=br.readLine())!=null){
				sb.append(line+ "\n");
			}
			is.close();
			str=sb.toString();
		}catch(Exception ex){
			Log.e("Buffered Error", ex.toString());
		}
		
		try{
			jo=new JSONObject(str);
		}catch(JSONException je){
			Log.e("Json Error", je.toString());
		}
		return jo;
	}
}
